package com.pumpkin.server.exception;

import com.pumpkin.server.commons.ResBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 *  全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public ResBean mySqlException(SQLException e) {
        if (e instanceof SQLIntegrityConstraintViolationException) {
            return ResBean.error("该数据有关联数据，操作失败！");
        }
        log.info("数据库异常，操作失败!：{}",e.getMessage());
        return ResBean.error("数据库异常，操作失败！");
    }
}